package com.nexgo.payment.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.nexgo.payment.R;
import com.nexgo.payment.util.BarUtils;

public abstract class BaseActivity extends AppCompatActivity {

    private MyDownTimer mDownTimer;
    private DownTimerListener mTimerListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setTransparentStatusBar21(this);
    }

    /**
     * 跳转到其它页面
     *
     * @param tarActivity
     * @author 谭忠扬-YuriTam
     * @date 2016年9月14日
     */
    protected void intent2Activity(Class<? extends Activity> tarActivity) {
        Intent intent = new Intent(BaseActivity.this, tarActivity);
        startActivity(intent);
    }

    /**
     * 跳转到其它页面-带参数
     *
     * @param tarActivity
     * @param bundle
     * @author 谭忠扬-YuriTam
     * @date 2016年9月14日
     */
    protected void intent2Activity(Class<? extends Activity> tarActivity, Bundle bundle) {
        Intent intent = new Intent(BaseActivity.this, tarActivity);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 跳转到其他界面
     *
     * @param tarActivity
     * @author 谭忠扬-YuriTam
     * @time 2017年5月5日
     */
    protected void intent2ActivityForResult(Class<? extends Activity> tarActivity) {
        Intent intent = new Intent(BaseActivity.this, tarActivity);
        startActivityForResult(intent, 1000);
    }

    /**
     * 跳转到其他界面，带参数
     *
     * @param tarActivity
     * @param bundle
     * @author 谭忠扬-YuriTam
     * @time 2017年5月5日
     */
    protected void intent2ActivityForResult(Class<? extends Activity> tarActivity, Bundle bundle) {
        Intent intent = new Intent(BaseActivity.this, tarActivity);
        intent.putExtras(bundle);
        startActivityForResult(intent, 1000);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    /**
     * 开始倒计时
     *
     * @param timeOut       倒计时时间
     * @param timerListener 倒计时回调
     */
    public void setDownTimer(long timeOut, DownTimerListener timerListener) {
        mTimerListener = timerListener;
        if (mDownTimer == null) {
            mDownTimer = new MyDownTimer(timeOut * 1000);
        }
        mDownTimer.start();
    }

    /**
     * 重新计时
     */
    public void restartDownTimer() {
        if (mDownTimer != null) {
            mDownTimer.cancel();
            mDownTimer.start();
        }
    }

    /**
     * 取消倒计时
     */
    public void cancelDownTimer() {
        if (mDownTimer != null) mDownTimer.cancel();
    }

    /**
     * 倒计时器
     */
    class MyDownTimer extends CountDownTimer {

        public MyDownTimer(long millisInFuture) {
            super(millisInFuture, 1000);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            if (mTimerListener != null) {
                mTimerListener.onTick(millisUntilFinished / 1000);
            }
        }

        @Override
        public void onFinish() {
            if (mTimerListener != null) {
                mTimerListener.onFinish();
            }
        }
    }

    /**
     * 倒计时回调接口，回调时间单位为秒
     */
    public interface DownTimerListener {

        void onTick(long secondsUntilFinished);

        void onFinish();
    }
}
